/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.transaction.bean.impl;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.RandomAccess;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.spring.transaction.bean.EnlistableResource;

/**
 * A list that participate to transactions. The elements of the list should be
 * immutable, otherwise the transaction behaviour won't be enforced.
 * 
 * @param <E>
 *          the type of the element of the list
 */
public class EnlistableResourceList<E>
    extends AbstractList<E>
    implements List<E>, EnlistableResource, RandomAccess
{

  /**
   * the base {@link AbstractEnlistableResource} that provide transactional
   * comportment.
   */
  private class EnlistableResourceListObject
      extends AbstractEnlistableResource<List<E>>
  {

    /**
     * constructor.
     * 
     * @param baseList
     *          the initial value of the transactional object.
     */
    public EnlistableResourceListObject(List<E> baseList)
    {
      super(baseList);
    }



    /**
     * clone the originalObject to generate a transactional view of it.
     * 
     * @param originalObject
     *          the object to clone
     * @return a clone of the originalObject.
     * @see com.genia.toolbox.spring.transaction.bean.impl.AbstractEnlistableResource#cloneObjectForTransaction(java.lang.Object)
     */
    @Override
    public List<E> cloneObjectForTransaction(List<E> originalObject)
    {
      return new ArrayList<E>(originalObject);
    }
  }

  /**
   * the delegate object for transactional comportment.
   */
  private transient final EnlistableResourceListObject enlistableResourceListObject;



  /**
   * constructor.
   */
  public EnlistableResourceList()
  {
    this(new ArrayList<E>());
  }



  /**
   * constructor.
   * 
   * @param baseList
   *          the initial value of this {@link List}.
   */
  public EnlistableResourceList(List<E> baseList)
  {
    enlistableResourceListObject = new EnlistableResourceListObject(baseList);
  }



  /**
   * Inserts the specified element at the specified position in this list
   * (optional operation). Shifts the element currently at that position (if
   * any) and any subsequent elements to the right (adds one to their indices).
   * <p>
   * 
   * @param index
   *          index at which the specified element is to be inserted.
   * @param element
   *          element to be inserted.
   * @see java.util.AbstractList#add(int, java.lang.Object)
   */
  @Override
  @Transactional
  public void add(int index, E element)
  {
    enlistableResourceListObject.getTransactionalView().getObjectForWriting().add(index, element);
  }



  /**
   * commit the transaction defined by {@link TransactionStatus}. A
   * {@link #prepare(TransactionStatus)} must have been called.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.basics.transaction.bean.EnlistableResource#commit(org.springframework.transaction.TransactionStatus)
   */
  public void commit(TransactionStatus status)
      throws TechnicalException
  {
    enlistableResourceListObject.commit(status);

  }



  /**
   * /** Returns the element at the specified position in this list.
   * 
   * @param index
   *          index of element to return.
   * @return the element at the specified position in this list.
   * @see java.util.AbstractList#get(int)
   */
  @Override
  @Transactional(readOnly = true)
  public E get(int index)
  {
    return enlistableResourceListObject.getTransactionalView().getObjectForReading().get(index);
  }



  /**
   * prepare the transaction defined by {@link TransactionStatus}. If this
   * method succeed, a {@link #commit(TransactionStatus)} must succeed or the
   * transactional integrity of the application won't be insured.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.basics.transaction.bean.EnlistableResource#prepare(org.springframework.transaction.TransactionStatus)
   */
  public void prepare(TransactionStatus status)
      throws TechnicalException
  {
    enlistableResourceListObject.prepare(status);
  }



  /**
   * Removes the element at the specified position in this list (optional
   * operation). Shifts any subsequent elements to the left (subtracts one from
   * their indices). Returns the element that was removed from the list.
   * <p>
   * 
   * @param index
   *          the index of the element to remove.
   * @return the element previously at the specified position.
   * @see java.util.AbstractList#remove(int)
   */
  @Override
  @Transactional
  public E remove(int index)
  {
    return enlistableResourceListObject.getTransactionalView().getObjectForWriting().remove(index);
  }



  /**
   * rollback the transaction defined by {@link TransactionStatus}.
   * 
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.basics.transaction.bean.EnlistableResource#rollback(org.springframework.transaction.TransactionStatus)
   */
  public void rollback(TransactionStatus status)
      throws TechnicalException
  {
    enlistableResourceListObject.rollback(status);
  }



  /**
   * Replaces the element at the specified position in this list with the
   * specified element (optional operation).
   * <p>
   * 
   * @param index
   *          index of element to replace.
   * @param element
   *          element to be stored at the specified position.
   * @return the element previously at the specified position.
   * @see java.util.AbstractList#set(int, java.lang.Object)
   */
  @Override
  @Transactional
  public E set(int index, E element)
  {
    return enlistableResourceListObject.getTransactionalView().getObjectForWriting().set(index, element);
  }



  /**
   * Returns the number of elements in this collection. If the collection
   * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
   * <tt>Integer.MAX_VALUE</tt>.
   * 
   * @return the number of elements in this collection.
   * @see java.util.AbstractCollection#size()
   */
  @Override
  @Transactional(readOnly = true)
  public int size()
  {
    return enlistableResourceListObject.getTransactionalView().getObjectForReading().size();
  }



  /**
   * start a new transaction for the current object.
   * 
   * @param definition
   *          {@link TransactionDefinition} instance (can be <code>null</code>
   *          for defaults), describing propagation behavior, isolation level,
   *          timeout etc
   * @param status
   *          the {@link TransactionStatus} of the transaction
   * @throws TechnicalException
   *           if an error occured
   * @see com.genia.toolbox.basics.transaction.bean.EnlistableResource#startTransaction(org.springframework.transaction.TransactionDefinition,
   *      org.springframework.transaction.TransactionStatus)
   */
  public void startTransaction(TransactionDefinition definition, TransactionStatus status)
      throws TechnicalException
  {
    enlistableResourceListObject.startTransaction(definition, status);
  }

}
